package com.mall.shopping.services;

import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.A;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service(interfaceClass = IProductService.class)
@Component
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductConverter productConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        // 参数基础检查

        request.requestCheck();

        Long productId = request.getId();

        Example example = new Example(Item.class);
        example.createCriteria()
                .andEqualTo("id",productId);
        Item item = itemMapper.selectOneByExample(example);

        ProductDetailDto productDetailDto = new ProductDetailDto();
        productDetailDto.setDetail("");
        productDetailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
        productDetailDto.setProductId(item.getId());
        productDetailDto.setProductImageBig(item.getImageBig());
        productDetailDto.setProductName(item.getTitle());
        productDetailDto.setSubTitle(item.getSellPoint());
        String[] images = item.getImages();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            list.add(images[i]);
        }
        productDetailDto.setProductImageSmall(list);
        productDetailDto.setSalePrice(item.getPrice());



        productDetailResponse.setProductDetailDto(productDetailDto);
        productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return productDetailResponse;
    }


    @Override
    public UpdateCartNumResponse freshCart(UpdateCartNumRequest updateCartNumRequest) {
        RedissonClient redissonClient = Redisson.create();
        RMap<Object, Object> map = redissonClient.getMap(updateCartNumRequest.getUserId().toString());
        CartProductDto cartProductDto = (CartProductDto) map.get("item_id_" + updateCartNumRequest.getItemId());
        cartProductDto.setProductNum(Long.valueOf(updateCartNumRequest.getNum()));
        cartProductDto.setChecked(updateCartNumRequest.getChecked());
        map.put("item_id_" + updateCartNumRequest.getItemId(),cartProductDto);

        UpdateCartNumResponse updateCartNumResponse = new UpdateCartNumResponse();
        updateCartNumResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        updateCartNumResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        return updateCartNumResponse;
    }

    @Override
    public List<ProductCateDto> getCategories() {
        List<ProductCateDto> productCateDtoList = itemCatMapper.selectCateList();
        return productCateDtoList;
    }
}
